package com.dcivision.webdav.util;

import org.w3c.dom.Node;

/**
 * A <code>QName</code> represents a fully-qualified name.
 */
public class QName {

  public static final String REVISION = "$Revision: 1.2 $";

  private String namespaceURI;
  private String localName;
  private int hashCode;

  public QName(String namespaceURI, String localName) {
    this.namespaceURI = (namespaceURI == null ? "" : namespaceURI).intern();
    this.localName = localName.intern();

    String hash1 = this.namespaceURI.hashCode() + "";
    String hash2 = this.localName.hashCode() + "";
    String hash3 = hash1 + '_' + hash2;
    this.hashCode = hash3.hashCode();
  }

  public String getNamespaceURI() {
    return this.namespaceURI;
  }

  public String getLocalName() {
    return this.localName;
  }

  public int hashCode() {
    return this.hashCode;
  }

  public boolean equals(Object obj) {
    return (obj != null
            && (obj instanceof QName)
            && namespaceURI == ( (QName) obj).getNamespaceURI()
            && localName == ( (QName) obj).getLocalName());
  }

  public boolean matches(Node node) {
    return (node != null)
        && (node.getNamespaceURI() != null)
        && (node.getLocalName() != null)
        && (node.getNamespaceURI().intern() == this.namespaceURI)
        && (node.getLocalName().intern() == this.localName);
  }

  public String toString() {
    return namespaceURI + ':' + localName;
  }
}
